package com.neusoft.elmboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.elmboot.dto.BusinessReqDto;
import com.neusoft.elmboot.mapper.BusinessMapper;
import com.neusoft.elmboot.po.Business;
import com.neusoft.elmboot.service.BusinessService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public List<Business> getBusinessList(BusinessReqDto params) {
        // 模拟数据库查询出的商家数据。
        List<Business> businessList = new ArrayList<>();

        Map<String,Object> pa = new HashMap<>();
        // 商家相关的查询参数
        pa.put("businessId",params.getBusinessId());
        pa.put("businessName",null == params.getBusinessName() ? null :
                params.getBusinessName().trim());
        // 点餐分类 参数：orderTypeId
        if (params.getOrderTypeId() != null) {
            pa.put("orderTypeId", params.getOrderTypeId());
        }
        // 分页相关的参数
        pa.put("beginIndex",params.getBeginIndex());
        pa.put("pageSize",params.getLimit());

        businessList = businessMapper.listAllBusiness(pa);

        return businessList;
    }

    @Override
    public Long getBusinessCount(BusinessReqDto params) {
        Map<String,Object> pa = new HashMap<>();
        // 商家相关的查询参数
        pa.put("businessId",params.getBusinessId());
        pa.put("businessName",null == params.getBusinessName() ? null :
                params.getBusinessName().trim());
        return businessMapper.listAllBusinessCount(pa);
    }

    @Override
    public PageInfo<Business> getBusinessPage(BusinessReqDto params) {
        Map<String,Object> pa = new HashMap<>();
        // 商家相关的查询参数
        pa.put("orderTypeId",params.getOrderTypeId());// 点餐类别
        pa.put("businessName",null == params.getBusinessName() ? null :
                params.getBusinessName().trim());

        PageHelper.startPage(params.getPage(), params.getLimit());
        List<Business>  businessList = businessMapper.listAllBusiness(pa);
        PageInfo<Business> pageInfo = new PageInfo<>(businessList);
        return pageInfo;
    }

    @Override
    public Integer addBusiness(BusinessReqDto params) {
        Business business = new Business();
        BeanUtils.copyProperties(params,business);
        return businessMapper.addBusiness(business);
    }

    @Override
    public Business getBusinessByBusinessId(Integer businessId) {
        List<Business> list = businessMapper.listBusinessByBusinessId(businessId);
        return null == list || list.size() == 0 ? null : list.get(0);
    }

    @Override
    public Integer updateBusiness(BusinessReqDto params) {
        Business business = new Business();
        BeanUtils.copyProperties(params,business);
        return businessMapper.updateBusinessById(business);
    }

    @Override
    public Integer deleteBusiness(Integer businessId) {
        return businessMapper.delBusiness(businessId);
    }
}
